package com.gla.Controller;

import com.gla.dao.CommodityMapper;
import com.gla.domain.*;
import com.gla.rabbitmq.DelayConstant;
import com.gla.rabbitmq.RabbitmqOrderProducer;
import com.gla.result.Result;
import com.gla.service.CommodityService;
import com.gla.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    OrderService orderService;
    @Resource
    CommodityService commodityService;
    @Autowired
    private RabbitmqOrderProducer orderProducer;

    @GetMapping("/getByUserId")
    public Result getOrderByUserId(@RequestParam Integer user_id, @RequestParam Integer state_id){
        List<OrderResponse> res = orderService.getOrderByUserId(user_id,state_id);
        return Result.succeed(res);
    }

    @PostMapping("/insertOrder")
    public Result insertOrder(@RequestBody List<OrderRequest> orderList){
        ArrayList<Integer> orderIdList = new ArrayList<>();
        //获取时间,转换为可插入数据库的时间格式
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = df.format(new Date());
        for(OrderRequest requestItem : orderList){
            Order order = requestItem.getOrder();
            order.setCreate_time(dateStr);
            //插入ORDER表,获取id
            Integer order_id = orderService.insertOrder(order);
            orderIdList.add(order_id);
            //插入ORDER DETAIL表
            for(OrderDetail item : requestItem.getList()){
                //获取库存
                Commodity commodity = commodityService.selectById(item.getCommodity_id());
                //库存足够的情况下
                if(commodity.getNumber() - item.getNumber() >= 0){
                    //插入订单详情
                    item.setOrder_id(order_id);
                    orderService.insertOrderDetail(item);
                    //减去库存
                    commodityService.updateNumber(item.getCommodity_id(),commodity.getNumber() - item.getNumber());
                } else {
                    //库存不足,删除订单
                    orderService.deleteOrderById(order_id);
                    return Result.fail("订单中有商品库存不足，失败");
                }
            }
            //进入消息队列
            orderProducer.publish(String.valueOf(order_id), String.valueOf(order_id),
                    DelayConstant.ORDER_DELAY_EXCHANGE, DelayConstant.ORDER_DELAY_ROUTING_KEY);
        }

        return Result.succeed(orderIdList);
    }

    @GetMapping("/deleteOrder")
    public Result delete(@RequestParam Integer order_id){
        orderService.deleteOrderById(order_id);
        return Result.succeed(order_id);
    }

    @GetMapping("/updateState")
    public Result updateStatus(@RequestParam Integer order_id,@RequestParam Integer state){
        return orderService.updateStatus(order_id,state);

    }
}
